﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="FileHelper.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace INNOVACALL.Framework.Common.FileHelpers
{
    /// <summary>
    /// Thanks to http://blog.brianbuikema.com/2010/04/asp-net-mvc-uploadingstoring-images-and-creating-thumbnails-example-included/
    /// </summary>
    public static class FileHelper
    {
        /// <summary>
        /// Resize image proportionaly and store it in filepath
        /// </summary>
        /// <param name="filePath">Thumbmail storage folder</param>
        /// <param name="filename">Original file name</param>
        /// <param name="resizedFilename">resizedFilename</param>
        /// <param name="percent">Resize ratio</param>
        public static void SaveResizedImage(string filePath, string filename, string resizedFilename, int percent)
        {
            Image origImg = Image.FromFile(Path.Combine(filePath, filename));
            Image resizedImg = ScaleByPercent(origImg, percent);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            resizedImg.Save(Path.Combine(filePath, resizedFilename), ImageFormat.Png);
            resizedImg.Dispose();
            origImg.Dispose();
        }
        /// <summary>
        /// Resize image to fit in a rectangle and store it in filepath
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="filename">The filename.</param>
        /// <param name="resizedFilename">The resized filename.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        public static void SaveResizedImage(string filePath, string filename, string resizedFilename, int width, int height)
        {
            Image origImg = Image.FromFile(Path.Combine(filePath, filename));
            Image resizedImg = FixedSize(origImg, width, height);
            if (!Directory.Exists(filePath)) {
                Directory.CreateDirectory(filePath);
            }
            resizedImg.Save(Path.Combine(filePath, resizedFilename), ImageFormat.Png);
            resizedImg.Dispose();
            origImg.Dispose();
        }

        /// <summary>
        /// Scales the by percent.
        /// </summary>
        /// <param name="imgPhoto">The img photo.</param>
        /// <param name="percent">The percent.</param>
        /// <returns>Image.</returns>
        public static Image ScaleByPercent(Image imgPhoto, int percent)
        {
            float nPercent = ((float)percent / 100);
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;

            int destX = 0;
            int destY = 0;
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

            grPhoto.Dispose();

            return bmPhoto;
        }

        /// <summary>
        /// Fixeds the size.
        /// </summary>
        /// <param name="imgPhoto">The img photo.</param>
        /// <param name="Width">The width.</param>
        /// <param name="Height">The height.</param>
        /// <returns>Image.</returns>
        public static Image FixedSize(Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.Red);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
            grPhoto.Dispose();

            return bmPhoto;
        }
    }
}
